package com.newhis.dao;

import com.newhis.common.DBHelper;
import com.newhis.entity.SpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * DAO层
 */
@Repository
public class MainDao {
    @Autowired
    //private JdbcTemplate jdbcTemplate;
    private DBHelper DBHelper;

    //@Override
    public SpUser getUserById(Integer id) {
        SpUser user = (SpUser)DBHelper.queryForObject("select user_id, username from sp_user where user_id = ?",
                new Object[]{id}, new BeanPropertyRowMapper(SpUser.class));
        return user;
    }

    public boolean setUserName(Integer id, String name) {
        int affected = DBHelper.update("update sp_user set username = ? where user_id = ?", new Object[]{name, id});
        if(affected == 1) {
            return true;
        } else {
            return false;
        }
    }

    public List<SpUser> getUserList() {
        List<SpUser> list = DBHelper.query("select user_id, username from sp_user",
                new Object[]{}, new BeanPropertyRowMapper(SpUser.class));
        return list;
    }
}
